const app = getApp();
import { request} from '../../utils/request';
import config from '../../utils/config'
Page({
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        picUrl:config.picUrl,
        formData: {album_id: 1},
        user:{},
        shop_name:'', //发行名称
        username:'', //账号
        password:'', //密码
        re_password:'',
        legal_person:'', //法人
        business_license:'', //营业执照
        brand_avatar:'', //发行logo
        phone:'', //公司电话
        shop_address:'', //公司地址
        card_photo_one:'', //身份证正面
        card_photo_two:'', //身份证反面
        story:'', //品牌故事
        extension:'',
        captcha:'',
        disabled:false,
        disabled2:false,
        btnTxt:"获取验证码",
        is_authentication:0,
        fail_reason:'',
        authentication_type:''
    },
    onLoad(options) {
        var timer = setInterval(()=>{
            var user = wx.getStorageSync('userInfo')
            // if(user.is_authentication == 1 || user.is_authentication == 2){
                this.setData({disabled:(user.is_authentication == 1 || user.is_authentication == 2) && user.authentication_type == 3})
            // }
            if(user){
                delete user.password
                this.setData({user})
                if(!user.extension){
                    var extension = wx.getStorageSync('extension')
                    if(extension){
                        this.setData({extension})
                    }
                }
                this.getData()
                clearInterval(timer)
            }
        },500)
    },
    changeValue(e){
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]:value})
    },
    getCode(){
        if(this.data.disabled){
            config.toast("你已认证通过或在申请中，不可重复申请",201,res=>{})
            return false
        }
        var phone = this.data.phone
        if(!phone){
            config.toast("请先填写手机号码后再获取验证码",201)
            return false
        }
        this.setData({disabled2:true})
        var num = 60;
        var timer = setInterval(()=>{
            if(num <= 1){
                this.setData({btnTxt:'获取验证码',disabled2:false})
                clearInterval(timer)
            }else{
                num--;
                this.setData({btnTxt:num+"秒后重试"})
            }
        },1000)
        request({
            url:"/miniprogram/system/sendSmsCode",
            data:{openid:this.data.user.openid,phone:phone},
        }).then(res=>{

        })
    },
    uploadImg(e) {
        var field = e.currentTarget.dataset.field
        this.upload(1, res => {
            if (res.code != 200) {
                wx.showToast({title: res.msg, icon: 'none'})
                return false
            }
            this.setData({[field]: res.data.src})
        })
    },
    delImg(e) {
        this.setData({[e.currentTarget.dataset.field]: ''})
    },
    upload(count, callback) {
        wx.showLoading()
        wx.chooseImage({
            count: count,
            sizeType: ['compressed', 'original'],
            success: res => {
                for (let v of res.tempFiles) {
                    wx.uploadFile({
                        url: config.url + '/system/api/uploadImage',
                        filePath: v.path,
                        name: 'file',
                        formData: this.data.formData,
                        success: res => {
                            wx.hideLoading()
                            callback(JSON.parse(res.data))
                        }
                    })
                }
            },
            fail: err => {
                wx.hideLoading()
            }
        })
    },
    submit(e){
        if(this.data.is_authentication == 2){
            config.toast("您已认证完成，无需重复认证。",201,rel=>{
                setTimeout(()=>{wx.navigateBack({delta:1})},1500)
            })
            return false
        }
        if(this.data.is_authentication == 1 && this.data.authentication_type == 3){
            config.toast("审核中，请勿修改。",201,rel=>{})
            return false
        }
        var {shop_name,username,password,re_password,legal_person,business_license,brand_avatar,phone,shop_address,card_photo_one, card_photo_two,story,extension,captcha} = this.data,
            user_id = this.data.user.id,
            member = e.currentTarget.dataset.member,
            authentication_type = 3
        if(password != re_password){
            config.toast("两次密码输入不一致",201)
            return false
        }
        if(!phone){
            config.toast("请输入您的手机号码",201)
            return false;
        }
        if(!captcha){
            config.toast("请输入您的短信验证码",201)
            return false;
        }
        if(member == 0){
            wx.showModal({
                title:"提示",
                content:"是否充值会员？",
                success:(res)=>{
                    if(res.confirm){
                        member = 1
                    }
                    request({
                        url:"/miniprogram/user/publisherAuth",
                        data:{shop_name,username,password,legal_person,business_license,brand_avatar,phone,shop_address,card_photo_one,card_photo_two,story,extension,captcha,user_id,authentication_type},
                        type:'post'
                    }).then(res=>{
                        config.toast(res.msg,201,rel=>{
                            if(member == 1 && res.code == 200){ //member=1时要跳转开通会员页面
                                wx.clearStorageSync()
                                wx.setStorageSync('userInfo',res.data)
                                setTimeout(()=>{wx.navigateTo({url: '/pages/member_recharge/member_recharge'})},1500)
                            }else if(res.code == 200 && member == 0){
                                console.log(1)
                                setTimeout(()=>{wx.navigateBack()},1500)
                            }
                        })
                    })
                }
            })
        }else{
            request({
                url:"/miniprogram/user/publisherAuth",
                data:{shop_name,username,password,legal_person,business_license,brand_avatar,phone,shop_address,card_photo_one,card_photo_two,story,extension,captcha,user_id,authentication_type},
                type:'post'
            }).then(res=>{
                config.toast(res.msg,201,rel=>{
                    if(member == 1 && res.code == 200){ //member=1时要跳转开通会员页面
                        wx.clearStorageSync()
                        wx.setStorageSync('userInfo',res.data)
                        setTimeout(()=>{wx.navigateTo({url: '/pages/member_recharge/member_recharge'})},1500)
                    }else if(res.code == 200 && member == 0){
                        console.log(1)
                        setTimeout(()=>{wx.navigateBack()},1500)
                    }
                })
            })
        }

    },
    getData(){
        request({
            url:"/miniprogram/user/getPublisherAuth",
            data:{user_id:this.data.user.id},
        }).then(res=>{
            if(res.code == 200){
                if([3,5].indexOf(res.data.authentication_type) == -1 && res.data.is_authentication == 2){
                    config.toast("您已认证为其他类型会员，无法认证发行商。",201,rel=>{
                        setTimeout(()=>{wx.navigateBack({delta:1})},1500)
                    })
                }else{
                    this.setData({
                        shop_name:res.data.shop_name,
                        username:res.data.username,
                        password:'',
                        re_password:'',
                        legal_person:res.data.legal_person,
                        business_license:res.data.business_license,
                        brand_avatar:res.data.brand_avatar,
                        phone:res.data.phone,
                        shop_address:res.data.shop_address,
                        card_photo_one:res.data.card_photo_one, 
                        card_photo_two:res.data.card_photo_two,
                        story:res.data.story,
                        extension:res.data.extension,
                        is_authentication:res.data.is_authentication,
                        fail_reason:res.data.fail_reason,
                        authentication_type:res.data.authentication_type
                    })
                    
                    this.setData({disabled:(res.data.is_authentication == 1 || res.data.is_authentication == 2) && res.data.authentication_type == 3})
                    
                    if(!res.data.extension){
                        var extension = wx.getStorageSync('extension')
                        if(extension){
                            this.setData({extension})
                        }
                    }
                }
                
            }
        })
    }
})